package com.fly.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.fly.dao.CheckItemDao;
import com.fly.entity.PageResult;
import com.fly.entity.QueryPageBean;
import com.fly.pojo.CheckItem;
import com.fly.service.CheckItemService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*
* 检查项服务
* */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemImpl implements CheckItemService {
    //注入Dao对象
    @Autowired
    private CheckItemDao checkItemDao;
    //新增检查项
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    //检查项分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();//当前页码
        Integer pageSize = queryPageBean.getPageSize();//每页记录数
        String queryString = queryPageBean.getQueryString();//查询条件

        //完成分页查询,基于mybatis提供的分页助手插件完成
        PageHelper.startPage(currentPage, pageSize);
        //条件查询
        Page<CheckItem> page =checkItemDao.selectByCondition(queryString);
        //获取总记录数
        long total = page.getTotal();
        //查询的数据
        List<CheckItem> rows = page.getResult();

        return new PageResult(total,rows);
    }

    //删除检查项
    @Override
    public void deleteById(Integer id) {
        //判断当前减产项是否关联到检查组,关联的话不允许删除
        Long countByCheckItemId = checkItemDao.findCountByCheckItemId(id);
        if (countByCheckItemId>0){
            //已经被关联到检查组不允许删除
            throw new RuntimeException();

        }else {
            checkItemDao.deleteById(id);
        }
    }

    //查询所有检查项
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }

    //编辑检查项
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    //编辑查询检查项回显数据
    public CheckItem findById(Integer id) {
       return checkItemDao.findById(id);
    }
}